#include <stdio.h>

int ack(int m, int n)
{
    if (m == 0)
        return n+1;

    if (n == 0)
        return ack(m-1, 1);

    return ack(m-1, ack(m, n-1));
}

int ack_2(int a, int b)
{
    int i;

    struct _node
    {
        int m;
        int n;
        int r;
    } node;

    struct _node nodelist[100];

    node.m = a;
    node.n = b;
    node.r = 0;
    nodelist[0] = node;

    i = 0;
    while (i >= 0)
    {
        if (nodelist[i].m == 0 && nodelist[i].n != -1)
        {
            nodelist[i].r = nodelist[i].n+1;
            while (--i >= 0)
            {
                if (nodelist[i].n == -1)
                {
                    nodelist[i].n = nodelist[i+1].r;
                    break;
                }
                else
                    nodelist[i].r = nodelist[i+1].r;
            }
        }
        else if (nodelist[i].n == 0)
        {
            node.m = nodelist[i].m-1;
            node.n = 1;
            node.r = 0;
            nodelist[++i] = node;
        }
        else
        {
            node.m = nodelist[i].m-1;
            node.n = -1;
            node.r = 0;
            nodelist[++i] = node;

            node.m = nodelist[i-1].m;
            node.n = nodelist[i-1].n-1;
            node.r = 0;
            nodelist[++i] = node;
        }
    }

    return nodelist[0].r;
}

int main(int argc, char *argv[])
{
    printf("%d\n", ack(3, 2));    
    printf("%d\n", ack_2(3, 2));    
    return 0;
}
